package com.spring.demo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ResourceService {

    @Autowired
    private ResourceLoader resourceLoader;

    public List<String> getList(String fileName) {
        List<String> list = new ArrayList<>();
        try {
            Resource resource = resourceLoader.getResource("classpath:" + fileName);
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    list.add(line);
                }
            }
        } catch (IOException e) {
            log.error("Error reading file from jar: ", e);
        }
        return list;
    }
}
